﻿namespace com.stifel.ProjectMgmtDao
{
    using System.Linq;
    using System.Text;

    using com.stifel.Extensions.DoubleExtensions;
    using com.stifel.Extensions.StringExtensions;

    public partial class StifelProject
    {
        #region Fields

        const string MMMDY = "MMM d, yyyy";
        const string NA = "n/a";
        const string YMD = "yyyy/MM/dd";

        #endregion Fields

        #region Properties

        public ProjectSite ParentSite
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public string GetAsTsv( )
        {
            StringBuilder sb = new StringBuilder( );
            string[ ] o = new string[ ] {
                this.Title,
                this.ProjectDescription.TrimNewLine().StripHTML( ).TrimNewLine(),
                this.GetProjectStatusAsString( ),
                this.GetDueDateAsYMD( ),
                this.GetStartDateAsYMD( ),
                this.GetCompletedDateAsYMD( ),
                this.OverallRanking.HasValue ? this.OverallRanking.Value.ToInt32( ).ToString( ) : 0.ToString( ),
                this.GetTeamsLeadAsString( ),
                this.GetTeamNameAsString( ),
                this.TeamRanking.HasValue ? this.TeamRanking.Value.ToInt32( ).ToString( ) : 0.ToString( )
            };
            foreach ( string s in o )
                sb.AppendFormat( "{0}\t", s );
            string q = sb.ToString( ).TrimEnd( '\t' );
            sb = null;
            return q;
        }

        public string GetAsTsvTitles( )
        {
            StringBuilder sb = new StringBuilder( );
            string[ ] o = new string[ ] {
                "Title",
                "Description",
                "Status",
                "Due Date",
                "Start Date",
                "Completed Date",
                "Overall Ranking",
                "Team Lead",
                "Team",
                "Team Ranking"
            };
            foreach ( string s in o )
                sb.AppendFormat( "{0}\t", s );
            string q = sb.ToString( ).TrimEnd( '\t' );
            sb = null;
            return q;
        }

        public string GetCompletedDateAsString( )
        {
            return CompletedDate.HasValue ? CompletedDate.Value.ToString( MMMDY ) : NA;
        }

        public string GetCompletedDateAsYMD( )
        {
            return CompletedDate.HasValue ? CompletedDate.Value.ToString( YMD ) : string.Empty;
        }

        public string GetDueDateAsString( )
        {
            return DueDate.HasValue ? DueDate.Value.ToString( MMMDY ) : NA;
        }

        public string GetDueDateAsYMD( )
        {
            return DueDate.HasValue ? DueDate.Value.ToString( YMD ) : string.Empty;
        }

        public string GetProjectStatusAsString( )
        {
            switch ( ProjectStatus.Value )
            {
                case ProjectMgmtDao.ProjectStatus.None:
                    return "n/a";
                case ProjectMgmtDao.ProjectStatus.Invalid:
                    return "incorrect";
                case ProjectMgmtDao.ProjectStatus.NotStarted:
                    return "Not started";
                case ProjectMgmtDao.ProjectStatus.InProgress:
                    return "In progress";
                case ProjectMgmtDao.ProjectStatus.Completed:
                    return "Completed";
                case ProjectMgmtDao.ProjectStatus.Deferred:
                    return "Deferred";
                default:
                    return "Unknown";
            }
        }

        public string GetStartDateAsString( )
        {
            return StartDate.HasValue ? StartDate.Value.ToString( MMMDY ) : NA;
        }

        public string GetStartDateAsYMD( )
        {
            return StartDate.HasValue ? StartDate.Value.ToString( YMD ) : string.Empty;
        }

        public string GetTeamNameAsString( )
        {
            return ParentSite.SiteTitle;
        }

        public string GetTeamsLeadAsString( )
        {
            StringBuilder sb = new StringBuilder( );
            TeamLeadImnName.ToList( ).ForEach( a => sb.AppendFormat( "{0}, ", a ) );
            string s = sb.ToString( ).Trim( ).TrimEnd( ',' );
            sb = null;
            return s;
        }

        public override string ToString( )
        {
            if ( ProjectStatus.HasValue )
                return string.Format( "{0} ({1})", Name, ProjectStatus.Value.ToString( ) );
            else
                return Name;
        }

        #endregion Methods
    }
}